{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: Aspect Extraction with Concepts"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: Aspect Extraction with Concepts\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM, NumericalConcept, StringConcept\n",
        "\n",
        "\n",
        "# Create a document instance\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"Service Agreement\\n\"\n",
        "        \"This Service Agreement is between DataFlow Solutions (Provider) and Enterprise Corp (Client).\\n\"\n",
        "        \"\\n\"\n",
        "        \"3. Payment Terms\\n\"\n",
        "        \"3.1 Service Fees\\n\"\n",
        "        \"The Client shall pay the Provider a monthly service fee of $5,000 for basic services. \"\n",
        "        \"Additional premium features are available for an extra $1,200 per month. \"\n",
        "        \"Setup fee is a one-time payment of $2,500.\\n\"\n",
        "        \"\\n\"\n",
        "        \"3.2 Payment Schedule\\n\"\n",
        "        \"All payments are due within 15 business days of invoice receipt. \"\n",
        "        \"Invoices will be sent on the first day of each month for the upcoming service period. \"\n",
        "        \"Late payments will incur a penalty of 2% per month on the outstanding balance.\\n\"\n",
        "        \"\\n\"\n",
        "        \"3.3 Payment Methods\\n\"\n",
        "        \"Payments may be made by bank transfer, corporate check, or ACH. \"\n",
        "        \"Credit card payments are accepted for amounts under $1,000 with a 3% processing fee. \"\n",
        "        \"Wire transfer fees are the responsibility of the Client.\\n\"\n",
        "        \"\\n\"\n",
        "        \"3.4 Refund Policy\\n\"\n",
        "        \"Services are non-refundable once delivered. However, if services are terminated \"\n",
        "        \"with 30 days notice, any prepaid fees for future periods will be refunded on a pro-rata basis.\\n\"\n",
        "    ),\n",
        ")\n",
        "\n",
        "# Define an aspect with associated concepts\n",
        "payment_aspect = Aspect(\n",
        "    name=\"Payment Terms\",\n",
        "    description=\"All clauses and provisions related to payment, including fees, schedules, methods, and policies\",\n",
        "    concepts=[\n",
        "        NumericalConcept(\n",
        "            name=\"Monthly Service Fee\",\n",
        "            description=\"The regular monthly fee for basic services\",\n",
        "            numeric_type=\"float\",\n",
        "        ),\n",
        "        NumericalConcept(\n",
        "            name=\"Premium Features Fee\",\n",
        "            description=\"Additional monthly fee for premium features\",\n",
        "            numeric_type=\"float\",\n",
        "        ),\n",
        "        NumericalConcept(\n",
        "            name=\"Setup Fee\",\n",
        "            description=\"One-time initial setup or onboarding fee\",\n",
        "            numeric_type=\"float\",\n",
        "        ),\n",
        "        NumericalConcept(\n",
        "            name=\"Payment Due Days\",\n",
        "            description=\"Number of days the client has to make payment after receiving invoice\",\n",
        "            numeric_type=\"int\",\n",
        "        ),\n",
        "        NumericalConcept(\n",
        "            name=\"Late Payment Penalty Rate\",\n",
        "            description=\"Percentage penalty charged per month for late payments\",\n",
        "            numeric_type=\"float\",\n",
        "        ),\n",
        "        StringConcept(\n",
        "            name=\"Accepted Payment Methods\",\n",
        "            description=\"List of payment methods that are accepted by the provider\",\n",
        "        ),\n",
        "        StringConcept(\n",
        "            name=\"Refund Policy\",\n",
        "            description=\"Conditions and procedures for refunds or credits\",\n",
        "        ),\n",
        "    ],\n",
        ")\n",
        "\n",
        "# Add the aspect to the document\n",
        "doc.add_aspects([payment_aspect])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract aspects and their concepts from the document\n",
        "doc = llm.extract_all(doc)\n",
        "\n",
        "# Access the extracted payment terms aspect and concepts\n",
        "payment_terms_aspect = doc.get_aspect_by_name(\"Payment Terms\")\n",
        "print(\"Extracted Payment Terms Section:\")\n",
        "for item in payment_terms_aspect.extracted_items:\n",
        "    print(f\"- {item.value}\")\n",
        "print(\"\\nExtracted Payment Details:\")\n",
        "for concept in payment_terms_aspect.concepts:\n",
        "    print(f\"\\n{concept.name}:\")\n",
        "    for item in concept.extracted_items:\n",
        "        print(f\"- {item.value}\")\n",
        "\n",
        "# Access specific extracted values\n",
        "monthly_fee = payment_terms_aspect.get_concept_by_name(\"Monthly Service Fee\")\n",
        "print(f\"\\nMonthly Service Fee: ${monthly_fee.extracted_items[0].value}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}